#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

. $(dirname $3)/conf_framework

# RHEL 7.3
check() {
	cur_name=$(basename $2)
	config_file_path=$1
	if compile_module $cur_name "struct queue_limits q;q.limits_aux" "linux/blkdev.h"
	then
		echo $cur_name "1" >> $config_file_path
	elif compile_module $cur_name "struct queue_limits q;q.max_write_zeroes_sectors" "linux/blkdev.h"
	then
		echo $cur_name "2" >> $config_file_path
	elif compile_module $cur_name "struct queue_limits q;q.max_write_same_sectors" "linux/blkdev.h"
	then
		echo $cur_name "3" >> $config_file_path
    else
        echo $cur_name "X" >> $config_file_path
    fi
}

apply() {
    case "$1" in
    "1")
		add_function "
	static inline void cas_copy_queue_limits(struct request_queue *exp_q,
			struct request_queue *cache_q, struct request_queue *core_q)
	{
		struct queue_limits_aux *l_aux = exp_q->limits.limits_aux;
		exp_q->limits = cache_q->limits;
		exp_q->limits.limits_aux = l_aux;
		if (exp_q->limits.limits_aux && cache_q->limits.limits_aux)
			*exp_q->limits.limits_aux = *cache_q->limits.limits_aux;
		exp_q->limits.max_sectors = core_q->limits.max_sectors;
		exp_q->limits.max_hw_sectors = core_q->limits.max_hw_sectors;
		exp_q->limits.max_segments = core_q->limits.max_segments;
		exp_q->limits.max_write_same_sectors = 0;
		if (queue_virt_boundary(cache_q))
			queue_flag_set(QUEUE_FLAG_NOMERGES, cache_q);
	}" ;;
    "2")
		add_function "
	static inline void cas_copy_queue_limits(struct request_queue *exp_q,
			struct request_queue *cache_q, struct request_queue *core_q)
	{
		exp_q->limits = cache_q->limits;
		exp_q->limits.max_sectors = core_q->limits.max_sectors;
		exp_q->limits.max_hw_sectors = core_q->limits.max_hw_sectors;
		exp_q->limits.max_segments = core_q->limits.max_segments;
		exp_q->limits.max_write_same_sectors = 0;
		exp_q->limits.max_write_zeroes_sectors = 0;
	}" ;;
	"3")
		add_function "
	static inline void cas_copy_queue_limits(struct request_queue *exp_q,
			struct request_queue *cache_q, struct request_queue *core_q)
	{
		exp_q->limits = cache_q->limits;
		exp_q->limits.max_sectors = core_q->limits.max_sectors;
		exp_q->limits.max_hw_sectors = core_q->limits.max_hw_sectors;
		exp_q->limits.max_segments = core_q->limits.max_segments;
		exp_q->limits.max_write_same_sectors = 0;
	}" ;;
    *)
        exit 1
    esac
}

conf_run $@
